package api

import (
	"encoding/json"
	"flag"
	"fmt"
	"time"
)

var (
	datetime string
	welcome  string
)

const (
	author string = "venki.chen"
)

type Test1 struct {
	Name string
	Age  int
}

type Test2 struct {
	name string
	Age  int
}

func init() {
	now := time.Now()
	datetime = now.Format("2006-01-02 15:04:05")
	welcome = author + "欢迎你来到今日的golang语言学习，现在的时间是：" + datetime
}

func Access() {
	fmt.Println(welcome)
	fmt.Println("-----------------")
	// flag包解析命令行参数
	// FlagLearn()

	// json练习
	// JsonLearnStruct()

	// JsonLearnMap()
	// JsonLearnSlice()
	// JsonLearnBase()

	// 练习结构体
	// StructLearn()

	// 反序列化
	// UnserializeLearn()

}

// UnserializeLearn 返回序列化操作
func UnserializeLearn() {
	// str := "{\"Name\":\"陈文小超\",\"Age\":28,\"Birthday\":\"1998-08-15\",\"Sal\":10,\"Skill\":\"运动\"}"
	str := "{\"monster_name\":\"陈文小超\",\"monster_age\":28,\"monster_birthday\":\"1998-08-15\",\"monster_sal\":10,\"monster_skill\":\"运动\"}"
	var monster Monster

	err := json.Unmarshal([]byte(str), &monster)

	if err != nil {
		fmt.Printf("unmarshaler err=%v\n", err)
	}

	fmt.Printf("反序列化后：%v\n", monster)
}

func StructLearn() {
	Test1 := Test1{
		Name: "venki.chen",
		Age:  28,
	}
	Test2 := Test2{
		name: "poet.chao",
		Age:  29,
	}
	fmt.Println(Test1)
	fmt.Println()
	fmt.Println(Test2)
}

// JsonLearnBase 基本数据类型序列化
func JsonLearnBase() {
	var number float64 = 199.65
	// 序列化
	data, err := json.Marshal(number)
	if err != nil {
		fmt.Printf("when order serialize err:%v\n", err)
	}
	fmt.Printf("monster 序列化后的结果：%v\n", string(data))
}

// JsonLearnSlice json练习-map
func JsonLearnSlice() {
	var slice []map[string]interface{}

	var m1 map[string]interface{}
	m1 = make(map[string]interface{})
	m1["name"] = "venki_1"
	m1["age"] = 281
	m1["address"] = "北京"
	slice = append(slice, m1)

	var m2 map[string]interface{}
	m2 = make(map[string]interface{})
	m2["name"] = "venki_2"
	m2["age"] = 282
	m2["address"] = "深圳"
	slice = append(slice, m2)

	// 序列化
	data, err := json.Marshal(slice)
	if err != nil {
		fmt.Printf("when order serialize err:%v\n", err)
	}
	fmt.Printf("monster 序列化后的结果：%v\n", string(data))
}

// JsonLearnMap json练习-map
func JsonLearnMap() {
	var venki map[string]interface{}
	venki = make(map[string]interface{})
	venki["name"] = "陈文小超"
	venki["age"] = 28
	venki["address"] = "深圳"

	// 序列化
	data, err := json.Marshal(venki)
	if err != nil {
		fmt.Printf("when order serialize err:%v\n", err)
	}
	fmt.Printf("monster 序列化后的结果：%v\n", string(data))
}

type Monster struct {
	Name     string  `json:"monster_name"`
	Age      int     `json:"monster_age"`
	Birthday string  `json:"monster_birthday"`
	Sal      float64 `json:"monster_sal"`
	Skill    string  `json:"monster_skill"`
}

// JsonLearnStruct json练习-struct
func JsonLearnStruct() {
	monster := Monster{
		Name:     "陈文小超",
		Age:      28,
		Birthday: "1998-08-15",
		Sal:      10.00,
		Skill:    "运动",
	}
	data, err := json.Marshal(&monster)
	if err != nil {
		fmt.Printf("when order serialize err:%v\n", err)
	}
	fmt.Printf("monster 序列化后的结果：%v\n", string(data))
}

// FlagLearn 解析命令行输入参数
func FlagLearn() {
	var user string
	var pwd string
	var host string
	var port int

	/**
	&user 用来接受用户命令行输入的-u后面的参数值
	"u" 就是-u指定参数
	“” 默认值
	“” 用户名，默认值为空
	*/
	flag.StringVar(&user, "u", "", "用户名，默认值为空")
	flag.StringVar(&pwd, "pwd", "", "密码，默认值为空")
	flag.StringVar(&host, "h", "localhost", "主机名，默认值为localhost")
	flag.IntVar(&port, "port", 3306, "端口号，默认值为3306")

	flag.Parse()

	fmt.Printf("user=%v\n,pwd=%v\n,host=%v\n,port=%v\n", user, pwd, host, port)
}
